import { AuditNode, AuditNodeLiving } from '@/pages/types/type';
import { ACCESSTOKEN } from '@/utils/authorize';
import { xinghua,pingfang,chongqingguoce,hubeismartworker,daliangeosmarthub,baogangecg } from '@/constants';
// export const  urlPrefix = `http://localhost:8080`;
// export const  urlPrefix = `http://localhost:65123`;
// export const urlPrefix = 'http://billschen.com';
// export const urlPrefix = 'http://pingfang.billschen.com';
// export const urlPrefix = 'http://xinghua.billschen.com';
// export const urlPrefix = 'http://kbnotebooks.billschen.com';
// export const obsPrifix = 'https://pingfangtd.obs.cn-south-1.myhuaweicloud.com'
// 2024-04-30 绑定了自域名：
// export const obsPrifix = 'http://xinghua-obs.billschen.com';

let url = '';
let obs = '';
if (pingfang) {
  url = 'http://pingfang.billschen.com';
  // obs = 'https://pingfangtd.obs.cn-south-1.myhuaweicloud.com';
  obs = 'http://pingfang-obs.billschen.com';
}
if (xinghua) {
  url = 'http://xinghua.billschen.com';
  obs = 'http://xinghua-obs.billschen.com';
}

if (chongqingguoce) {
  url = 'http://chongqingguoce.billschen.com';
  obs = 'http://chongqingguoce-obs.billschen.com';
}

if(hubeismartworker){
  url = 'http://hubeismartworker.billschen.com';
  obs = 'http://hubeismartworker-obs.billschen.com';
}


if(daliangeosmarthub){
  url = 'http://daliangeosmarthub.billschen.com';
  obs = 'http://daliangeosmarthub-obs.billschen.com';
}
if(baogangecg){
  url = 'http://baogangecg.billschen.com';
  // url = 'http://116.63.66.174:35086';
  // url = 'http://localhost:35086';
  obs = 'http://baogangecg-obs.billschen.com';
}

export const urlPrefix = url;
export const obsPrifix = obs;

export function getRequestInit(): RequestInit {
  const headers = {
    Authorization: `Bearer ${localStorage.getItem(ACCESSTOKEN)}`,
    'Content-Type': 'application/json',
  }; // { 'Authorization': `Bearer ${localStorage.getItem(ACCESSTOKEN)}` }
  const requestInit: RequestInit = {
    mode: 'cors',
    cache: 'no-cache',
    credentials: 'same-origin',
    redirect: 'follow',
    method: 'get',
    headers,
  };
  return requestInit;
}
export function postRequestInit(data: any): RequestInit {
  const headers = {
    Authorization: `Bearer ${localStorage.getItem(ACCESSTOKEN)}`,
    'Content-Type': 'application/json',
  }; // { 'Authorization': `Bearer ${localStorage.getItem(ACCESSTOKEN)}` }
  const requestInit: RequestInit = {
    mode: 'cors',
    cache: 'no-cache',
    credentials: 'same-origin',
    redirect: 'follow',
    method: 'post',
    headers,
    body: JSON.stringify(data),
  };
  return requestInit;
}

export const unwrapMySelf = (x: { t: any; children?: [] }): any => {
  if (x.children && x.children.length > 0) {
    return { ...x.t, children: x.children.map((c) => unwrapMySelf(c)) };
  }
  return { ...x.t };
};

export const unwrapt = (arr: { t: any; children?: [] }[]): any => {
  return arr.map((a) => unwrapMySelf(a));
};

export const addValueLabel = (x: {
  name: string;
  id: number;
  children?: [];
}): any => {
  if (x.children && x.children.length > 0) {
    return {
      ...x,
      label: x.name,
      value: 'c' + x.id,
      children: x.children.map((c) => addValueLabel(c)),
    };
  }
  const { children, ...other } = x;
  console.log(children);
  return { ...other, label: x.name, value: '' + x.id };
};

export const addValueLabelToAuditNode = (x: AuditNode): AuditNode => {
  const { leader, level, name, children } = x;
  const isGroup = level === 'group';
  const tip = `[${x.id}]${name}(${isGroup ? '分组' : leader.name})`;
  if (children && children.length > 0) {
    return {
      ...x,
      label: tip,
      value: '' + x.id,
      children: children.map((c) => addValueLabelToAuditNode(c)),
    };
  } else {
    const { children, ...other } = x;
    console.log(children);
    return { ...other, label: tip, value: '' + x.id };
  }
};

export const addValueLabelToAuditNodeLiving = (
  x: AuditNodeLiving,
): AuditNodeLiving => {
  const { name, children } = x;
  if (children && children.length > 0) {
    return {
      ...x,
      label: name,
      value: '' + x.id,
      children: children.map((c) => addValueLabelToAuditNodeLiving(c)),
    };
  } else {
    const { children, ...other } = x;
    console.log(children);
    return { ...other, label: name, value: '' + x.id };
  }
};
